<!DOCTYPE html>
<html>
<head lang="zh-CN">
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title></title>
    <link rel="shortcut icon" type="image/x-icon" href=""/>
</head>
<body>
<div style="text-align: center">
    <h1>WebSocket测试</h1>
</div>
</body>
<script type="text/javascript">
    var wsServer = 'ws://localhost/demo'; //服务器地址
    var websocket = new WebSocket(wsServer); //创建WebSocket对象
    websocket.send("hello");//向服务器发送消息
    alert(websocket.readyState);//查看websocket当前状态
    websocket.onopen = function (evt) {
                  //已经建立连接


    };
    websocket.onclose = function (evt) {
                  //已经关闭连接


    };
    websocket.onmessage = function (evt) {
                  //收到服务器消息，使用evt.data提取


    };
    websocket.onerror = function (evt) {
                  //产生异常


    };
</script>

</html>